<template>
    <div class="login-container">
        <van-nav-bar class="page-nav-bar" title="登录">
            <template #left>
                <van-icon name="cross" @click="$router.back()" />
            </template>
        </van-nav-bar>
        <van-form @submit="onSubmit" ref="loginForm">
            <van-field v-model="user.mobile" name="mobile" placeholder="请输入手机号" :rules="userFormRules.mobile"
                type="number" maxlength="11">
                <!-- <i slot="left-icon" class="toutiao toutiao-shouji"></i> -->
                <template #left-icon>
                    <i class="toutiao toutiao-shouji"></i>
                </template>
            </van-field>
            <van-field v-model="user.code" name="code" placeholder="请输入验证码" :rules="userFormRules.code" type="number"
                maxlength="6">
                <template #left-icon>
                    <i class="toutiao toutiao-yanzhengma"></i>
                </template>
                <template #button>
                    <van-button type="default" round class="send-btn" @click="getVerificationCode" native-type="button"
                        v-if="isShowCountDown === false">
                        获取验证码</van-button>
                    <van-count-down :time="time" format="ss 秒" v-else @finish="finishCountDown" />
                </template>
            </van-field>
            <div class="login-btn">
                <van-button block type="info" native-type="submit" class="login-btn-area">登录</van-button>
            </div>
        </van-form>
    </div>
</template>

<script>
import { login, sendVerificationCode } from '../../api/user'

export default {
    name: 'LoginIndex',
    data() {
        return {
            user: {
                mobile: '13488888888',
                code: '246810',
            },
            userFormRules: {
                mobile: [
                    {
                        required: true,
                        message: '手机号不能为空'
                    },
                    {
                        pattern: /^1[3|5|7|9]\d{9}$/,
                        message: '手机号格式错误'
                    }
                ],
                code: [
                    {
                        required: true,
                        message: '验证码不能为空'
                    },
                    {
                        pattern: /^\d{6}$/,
                        message: '验证码格式错误'
                    }
                ]
            },
            time: 60 * 1000,
            isShowCountDown: false
        };
    },
    methods: {
        async onSubmit() {
            const user = this.user
            this.$toast.loading({
                message: '登录中...',
                forbidClick: true,
                duration: 0
            });
            try {
                const res = await login(user)
                this.$toast.success('登录成功')
                this.$store.commit('setUser', res.data.data)
                this.$router.back()
            } catch (err) {
                console.log('登录失败', err);
                if (err.response.status === 400) {
                    this.$toast.fail('手机号或验证码错误')
                } else {
                    this.$toast.fail('登录失败，请稍后重试')
                }
            }
        },
        async getVerificationCode() {
            try {
                await this.$refs.loginForm.validate('mobile')
                this.isShowCountDown = true

            } catch (err) { }

            try {
                let res = await sendVerificationCode(this.user.mobile)
            } catch (err) {
                his.isShowCountDown = false
                if (err.response.status === 429) {
                    this.$toast('发送太频繁，请稍后重试')
                } else {
                    this.$toast('发送失败，请稍后重试')
                }
            }
        },
        finishCountDown() {
            this.isShowCountDown = false
        }
    },
}
</script>

<style scoped lang="less">
.toutiao {
    font-size: 37px;
}

.send-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #efefef;
    white-space: nowrap;
    font-size: 22px;
    color: #666;
}

.login-btn {
    padding: 53px 33px;

    .login-btn-area {
        background-color: #6db4fb;
        border: none;
    }
}
</style>